﻿$(document).ready(function () {
    page.init();
});

var page = {
    pageIndex: 0,
    //pageSize: 10000,
    url: "/handler/reward.ashx?action=",
    editor: null,

    init: function () {
        $(".top-bar .right li:eq(2)").addClass("current");

        $(".tabs dd").click("click", function () {
            $(this).parent().find("dd").removeClass("active");
            $(this).addClass("active");

            page.showResponse(0);
        });

        $("#secondPanel").css({ left: $(window).width() / 2 + 250 });
        $(".tabs dd:eq(0)").addClass("active").click();
    },

    showResponse: function (pageIndex) {
        if (pageIndex != undefined)
            page.pageIndex = pageIndex;
        $.get(page.url + "response", { never: $(".tabs dd.active").index() == 0, order: "asc", id: page.id, pageIndex: page.pageIndex, pageSize: page.pageSize, r: Math.random() }, function (data) {
            if (data.error) {
                alert(data.error);
                return false;
            }
            $("#result").html(TrimPath.processDOMTemplate("responselist", data));
            //page.pager(data.count);

            $("#tryAmount").html(data.amount);

            $(".amount").focus(function () {
                $(this).parent().parent().find(".ignore").hide();
                $(this).parent().parent().find(".sure").show();
            }).keyup(function () {
                $(this).val($(this).val().replace(/\D|^0/g, ''));
            }).bind("paste", function () {
                $(this).val($(this).val().replace(/\D|^0/g, ''));
            }).blur(function () {
                if ($(this).val() == "") {
                    $(this).parent().parent().find(".ignore").show();
                    $(this).parent().parent().find(".sure").hide();
                }
                else {
                    $(this).parent().parent().find(".ignore").hide();
                    $(this).parent().parent().find(".sure").show();
                }
            }).css("ime-mode", "disabled");
        }, "json");
    },

    pager: function (count) {
        if (count == 0) {
            $("#pagination").empty();
            return;
        }
        $("#pagination").pagination(count, {
            num_edge_entries: 2,
            num_display_entries: 10,
            current_page: page.pageIndex,
            items_per_page: page.pageSize,
            callback: page.showResponse
        });
    },

    finish: function () {
        //page.id
        if (confirm("您确定结束当前悬赏的评审么？\r\n倘若存在未评审创意，则默认设为不采纳")) {
            $.post(page.url + "finish", { id: page.id }, function (data) {
                if (data.success)
                    window.location.reload();
                else
                    alert(data.error);
            }, "json");
        }
    },

    ignore: function (responseId, rewardId, obj) {
        $.post(page.url + "review", { id: responseId, rewardId: rewardId, amount: 0 }, function (data) {
            if (data.error) {
                alert(data.error);
                return false;
            }

            if ($(".tabs dd.active").index() == 0)
                $(obj).parents(".responsePanel").remove();
            else {
                $(obj).parent().html('不采纳');
            }
        }, "json");
    },

    sure: function (responseId, rewardId, obj) {
        var amount = $("#amount_" + responseId).val();
        var tem = page.rewardAmount - parseInt($("#tryAmount").html());
        if (amount > tem) {
            alert("目前可奖赏素币为 " + tem);
            return false;
        }
        $.post(page.url + "review", { id: responseId, rewardId: rewardId, amount: amount }, function (data) {
            if (data.error) {
                alert(data.error);
                return false;
            }

            if ($(".tabs dd.active").index() == 0)
                $(obj).parents(".responsePanel").remove();
            else {
                $(obj).parent().html('奖赏素币：' + amount);
            }
            $("#tryAmount").html(parseInt($("#tryAmount").html() + data.list[i].Amount));
        }, "json");
    }
};

